using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Security.Principal;
using Universum.Model.Services.Blog;
using Universum.Model.Domain.Blogs;
using Universum.Infrastructure.Utils;
using StructureMap;
using Universum.Model.Services;
using System.Text;

namespace Universum.Controllers
{
	[Authorize( Roles="teacher,student")]
	public class BlogArticleController : UniversumController
	{
		public static readonly string ARTICLE_ID = "article-id";
		public static readonly string ARTICLE_SLUG = "article-slug";
		public static readonly string ARTICLE_TITLE = "article-title";
		IBlogArticleService _Service;
		IFriendlyUrlService _FriendlyUrlService;
		public BlogArticleController(
			System.Security.Principal.IPrincipal principal,
			IBlogArticleService service,
			IFriendlyUrlService urlService
			)
			: base(principal)
		{
			_Service = service;
			_FriendlyUrlService = urlService;
		}

		//
		// GET: /BlogArticle/Details/5

		public ActionResult Details(Guid id)
		{
			var model = _Service.GetArticleById(id);
			ViewData.Model = model;
			return View();
		}

		//
		// GET: /BlogArticle/Create

		public ActionResult Create()
		{
			
			return View();
		}

		//
		// POST: /BlogArticle/Create
		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(FormCollection collection)
		{
			try
			{
				// TODO: Add insert logic here
				BlogArticle article = _Service.Create();
			    UpdateModel(article,
					new string[]{
						MemberUtil<BlogArticle>.MemberName(a=>a.Publish),

						MemberUtil<BlogArticle>.MemberName(a=>a.Text),
						MemberUtil<BlogArticle>.MemberName(a=>a.Title),

					},
					collection.ToValueProvider());
				article.Slug =
					_FriendlyUrlService.CreateSlugFrom(article.Title);
					//ObjectFactory.GetInstance<IFriendlyUrlService>().CreateSlugFrom(article.Title);
				_Service.SaveOrUpdate(article);
				return RedirectToAction<BlogArticleController>(c => c.Show(article.Slug));
			}
			catch
			{
				return View();
			}
		}

		//
		// GET: /BlogArticle/Edit/5

		public ActionResult Edit(Guid id)
		{
			var model = _Service.GetArticleById(id);
			if (CurrentUsername.Equals(model.Username))
				ViewData.Model = model;
			else
				return RedirectToAction<BlogArticleController>(
					c => c.Create());
			return View();
		}

		//
		// POST: /BlogArticle/Edit/5
		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit(Guid id, FormCollection collection)
		{
			try
			{
				var article = _Service.GetArticleById(id);
				// TODO: Add update logic here
				UpdateModel(article,
					new string[]{
						MemberUtil<BlogArticle>.MemberName(a=>a.Publish),

						MemberUtil<BlogArticle>.MemberName(a=>a.Text),
						MemberUtil<BlogArticle>.MemberName(a=>a.Title),

					},
					collection.ToValueProvider());
				article.Slug = ObjectFactory.GetInstance<IFriendlyUrlService>().CreateSlugFrom(article.Title);
				_Service.SaveOrUpdate(article);
				return RedirectToAction<BlogArticleController>(controller => controller.Show(article.Slug));
			}
			catch
			{
				return View();
			}
		}


		//
		// Post /BlogArticle/Remove/5
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Remove(Guid id)
		{
			var article = _Service.GetArticleById(id);
			if (article.Username.Equals(CurrentUsername))
				_Service.RemoveArticleById(id);

			return RedirectToAction<BlogsController>(
					controller => controller.Index());

		}
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult SaveComment(BlogCommentArticle comment, FormCollection extras)
		{
			comment.ID = Guid.NewGuid();
			comment.Created = comment.Publish = DateTime.Now;
			comment.Title = "Reply to " + extras[ARTICLE_TITLE];
			//comment.Slug = ObjectFactory.GetInstance<IFriendlyUrlService>().CreateSlugFrom(comment.Title);
			comment.Slug = new StringBuilder(comment.ID.ToInt().ToString()).Append('-').Append(comment.Title).ToString().AsSlug();
			comment.Username = CurrentUsername;
			string articleSlug = extras[ARTICLE_SLUG];
			if (comment.Text.IsNullOrEmpty())
			{ // the comment is ignored
				return RedirectToAction<BlogArticleController>(
				c => c.Show(articleSlug));
			}
			_Service.SaveComment(comment);
			

			return RedirectToAction<BlogArticleController>(
				c => c.Show(articleSlug));


		}
		public ActionResult Show(string id)
		{
			var article = _Service.GetArticleBySlug(id);

			var model = new Universum.Views.BlogArticle.BlogArticleShowViewModel();
			model.ViewData = new ViewDataDictionary();
			model.ViewData.Add(ARTICLE_ID, article.ID);
			model.ViewData.Add(ARTICLE_SLUG, article.Slug);
			model.ViewData.Add(ARTICLE_TITLE, article.Title);
			model.Article = article;
			model.NextComment = new BlogCommentArticle
			{
				ID = Guid.NewGuid()
				
			};
			model.Comments = _Service.GetCommentsByArticleId(article.ID);
			ViewData.Model = model;
			return View();
		}
	}
}
